from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from .neo4j_connection import graph
from .api_instance import api_instance
from rasa.core.channels.socketio import *
from socketIO_client import SocketIO

class ActionAskAboutPerson(Action):
    def name(self) -> Text:
        return "action_ask_about_person"

    async def run(self, dispatcher: CollectingDispatcher,
                  tracker: Tracker,
                  domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        person_name = next(tracker.get_latest_entity_values("person"), None)

        if not person_name:
            api_instance.ask(tracker.events, dispatcher)
            return []

        person_list = graph.query_person(person_name)

        if not person_list:
            dispatcher.utter_message("不好意思，我不清楚你问的" + person_name + "是谁。", attachment="finish")
            return []

        person_node = person_list[0]["p"]
        message = (person_name
                   + ("我们叫他" + person_node["alias"] + "，" if "alias" in person_node else "")
                   + ("在我们这里是" + person_node["position"] + "。" if "position" in person_node else "")
                   + person_node["des"])

        api_instance.optimize_response(tracker.events, message, dispatcher)

        return []
